const MD5 = require('md5.js');
// 服务器地址
// 正式版
const host = "https://dx.mz-api.yeting0725.cn"; //api.meizhuang.whudong.cn
// 测试版sss
// const host = 'https://t.api.hezi.zhunzha.com'
const come_from = 1
const get_time = function() {
	var d = new Date();
	var time = d.getTime() / 1000;
	return parseInt(time);
}

const apiToken = function(timeDiff) {
	var secret_key = 's3f1sdf12ds3sf31sf6sf54s6f4ewrdfg46f'; // 服务端生成的 secret_key
	var timestamp = (get_time() + timeDiff).toString(); // 取时间戳
	timestamp = timestamp.slice(0, -2);
	return MD5.md5(secret_key + timestamp);
}

const wxRequest = (params, url) => {
	var requestData = params.data || [];
	requestData.token = wx.getStorageSync('token');
	requestData.openid = wx.getStorageSync('openid');
	requestData.uid = wx.getStorageSync('uid') || 0;
	requestData.come_from = come_from
	wx.request({
		url: url,
		method: params.method || "POST",
		data: requestData,
		header: {
			"Cotent-Type": 'application/json',
			"api-token": apiToken(wx.getStorageSync('timeDiff'))
		},
		success: (res) => {
			if (res.statusCode == 200) {
				// if (!res.data.token) {} else {
				//   wx.setStorageSync('token', res.data.token);
				// }
				if (res.data.code == 50003 || res.data.code == 50004 || res.data.code == 10000) {
					params.success && params.success(res);
				}
				if (res.data.code == 50002) {
					params.success && params.success(res);
				}
				if (res.data.code == 70000) {
					params.success && params.success(res);
					return;
				}
				//统一提示，登录错误、官方公告等
				if (res.data.code != 0) {
					wx.showToast({
						title: res.data.error,
						icon: 'none',
						duration: 2000,
						success: function(e) {

						}
					})
					return;
				}
				params.success && params.success(res)
			} else if (res.statusCode == 504) {
				wx.showToast({
					title: "请求超时，请重试",
					icon: 'none',
					duration: 2000
				})
			} else {
				wx.showToast({
					title: "网络错误",
					icon: 'none',
					duration: 2000
				})
			}
		},
		fail: (res) => {
			console.log(911, res)
		},
		complete: (res) => {

		}
	})
}


// 储存用户信息
const saveUserInfo = (params) => wxRequest(params, host + '/user/save_userinfo')
//首页列表
const goodsList = (params) => wxRequest(params, host + '/goods/goods_list')
//商品详情
const goodDetail = (params) => wxRequest(params, host + '/goods/goods_detail')
//订单列表
const orderList = (params) => wxRequest(params, host + '/exchange/exchange_list')
//常见问题
// const commonProblem = (params) => wxRequest(params,'https://api.hdcj.9w9.com/article/list')
//兑换接口
const exchange_goods = (params) => wxRequest(params, host + '/goods/exchange_goods')
// 确认收货接口
const confirm_receipt = (params) => wxRequest(params, host + '/exchange/confirm_receipt')
//存储formid
const saveFrormid = (params) => wxRequest(params, host + '/formid/save_formid')
//我的红包
const myRedPacket = (params) => wxRequest(params, host + '/goods/my_red_packet')
//退货接口
const exchangeApply_refund = (parms) => wxRequest(parms, host + '/exchange/apply_refund')
// 获取七牛云token
const getQnyToken = (parms) => wxRequest(parms, host + '/qny/qny_token')
// 投诉
const complain = (parms) => wxRequest(parms, host + '/complain/complain')
module.exports = {
	host,
	saveUserInfo,
	goodsList,
	goodDetail,
	orderList,
	exchange_goods,
	confirm_receipt,
	saveFrormid,
	myRedPacket,
	exchangeApply_refund,
	come_from,
	getQnyToken,
	complain
	// commonProblem
}
